package com.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProductBean;
import com.project.service.IProductService;
import com.project.service.IPurchaseCarService;
import com.project.util.InfoResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author lyl
 * description:
 */
@RestController
@RequestMapping("product")
@Api(tags = "商品相关controller")
public class ProductController {
    @Autowired
    private IProductService iProductService;
    @Autowired
    private IPurchaseCarService iPurchaseCarService;


    @GetMapping("showProductByPage/{pageNum}/{productName}/{productType}/{startNum}/{endNum}")
    private Page<ProductBean> showProductByPage(@PathVariable int pageNum,
                                                @PathVariable String productName,
                                                @PathVariable String productType,
                                                @PathVariable int startNum,
                                                @PathVariable int endNum) {
        return iProductService.showProductByPage(pageNum, productName, productType, startNum, endNum);
    }

    @GetMapping("findProductById/{productId}")
    private ProductBean findProductById(@PathVariable int productId) {
        return iProductService.findProductById(productId);
    }

    @PostMapping("updateProductPrice/{productId}/{salePrice}")
    private InfoResult updateProductPrice(@PathVariable int productId, @PathVariable int salePrice) {
        return iProductService.updateProductPrice(productId, salePrice);
    }

    @PostMapping("uploadImg")
    private String uploadIng(@RequestParam("imgPath") MultipartFile file) {
        //得到上传文件名
        String fileName = file.getOriginalFilename();
        //将文件名以时间毫秒数重命名
        fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
        File f = new File("d:/img/" + fileName);
        try {
            //将文件上传至指定位置
            file.transferTo(f);
        } catch (IOException e) {
            return "no";
        }
        //返回图片名称
        System.out.println(fileName);
        return fileName;
    }


    @PostMapping("addNewProduct/{productName}/{factory}/{productType}/{purchasePrice}/{salePrice}/{imgPath}/{purchaseNum}/{userId}/{supplierId}")
    private InfoResult addNewProduct(@PathVariable String productName,
                                     @PathVariable String factory,
                                     @PathVariable String productType,
                                     @PathVariable long purchasePrice,
                                     @PathVariable long salePrice,
                                     @PathVariable String imgPath,
                                     @PathVariable int purchaseNum,
                                     @PathVariable int userId,
                                     @PathVariable int supplierId) {
        ProductBean productBean = new ProductBean(productName, purchasePrice, salePrice, factory, productType, imgPath, 0);
        long productId = iProductService.addNewProduct(productBean);
        int x = iPurchaseCarService.addPurchaseToCar(userId, supplierId, productId, purchaseNum);
        if (x == 1) {
            return new InfoResult(0, "添加成功");
        }
        return new InfoResult(1, "添加失败");
    }
}
